#include "client_server.h"

#include "ulog.h"
//tcp 服务，连接手机app和其他客户端
static struct tcpserver *serv;
static uint8_t buf[1024];
#define client_ip "0.0.0.0"
#define clinet_port 6666


static void tcpserver_event_notify(tcpclient_t client, uint8_t event)
{
    int ret;
    switch (event)
    {
    case TCPSERVER_EVENT_CONNECT:
        LOG_D("client connect:%d", client->sock);
        break;
    case TCPSERVER_EVENT_RECV:
        ret = tcpserver_recv(client, buf, 1024, -1);
        clinet_handle_msg(client,buf,ret);
        if (ret > 0)
        {
            ret = tcpserver_send(client, buf, ret, 0);
        }
        break;
    case TCPSERVER_EVENT_DISCONNECT:
        LOG_D("client disconnect:%d", client->sock);
        break;
    default:
        break;
    }
}

uint8_t client_servre_init()
{
    serv = tcpserver_create(client_ip, clinet_port);

    tcpserver_set_notify_callback(serv, tcpserver_event_notify);
}
uint8_t client_servre_uninit()
{
    tcpserver_destroy(serv);
    return 0;
}

uint8_t clinet_handle_msg(tcpclient_t client,uint8_t*data,int len)
{
    //解析命令，并执行
    return 0;
}
